package com.yxx.strategy.uploadStrategyImpl;

import com.qiniu.http.Response;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.yxx.config.properties.QiniuProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * qiniu上传策略
 * @author yuxiangxun
 * @date 2025/1/27
 * @apiNote
 */
@Slf4j
@Service("qiniuUploadStrategyImpl")
public class QiniuUploadStrategyImpl extends AbstractUploadStrategyImpl{

    @Resource
    private QiniuProperties qiniuProperties;

    /**
     * 判断文件是否存在
     * @param filePath 文件路径
     * @return
     */
    @Override
    public Boolean exists(String filePath) {
        return false;
    }

    /**
     * 上传
     * @param path        路径
     * @param fileName    文件名
     * @param inputStream 输入流
     * @throws IOException
     */
    @Override
    public void upload(String path, String fileName, InputStream inputStream) throws IOException {
        try {
            // 处理现有两种格式： 1. /  -->  /   2. /xxx/  -->  xxx/
            if(path.length() > 1 && path.charAt(0) == '/'){
                path = path.substring(1);
            }
            // 认证信息实例
            Auth auth = Auth.create(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey());
            // 构建一个上传七牛工具实例
            UploadManager uploadManager = new UploadManager(qiNiuConfig());
            // 设置上传后的文件名称
            String key = path + fileName;
            // 生成上传 token
            String upToken = auth.uploadToken(qiniuProperties.getBucketName());
            // 上传图片文件
            Response putFileResponse = uploadManager.put(inputStream, key, upToken, null, null);
            if(!putFileResponse.isOK()){
                throw new RuntimeException("上传七牛云出错："+putFileResponse);
            }
        }catch (Exception e){
            log.error("上传到七牛云出错啦，错误信息为：{}",e.getMessage());
        }
    }

    /**
     * 获取文件访问url
     * @param filePath 文件路径
     * @return
     */
    @Override
    public String getFileAccessUrl(String filePath) {
        // 处理文件路径格式，七牛云文件类型举例： test/plain
        // 处理现有两种格式： 1. /  -->  /   2. /xxx/  -->  xxx/
        if(filePath.length() > 1 && filePath.charAt(0) == '/'){
            filePath = filePath.substring(1);
        }
        return qiniuProperties.getUrl() + filePath;
    }

    private com.qiniu.storage.Configuration qiNiuConfig(){
        switch(qiniuProperties.getRegion()){
            case "huadong":
                return new com.qiniu.storage.Configuration(Region.huadong());
            case "huabei":
                return new com.qiniu.storage.Configuration(Region.huabei());
            case "huanan":
                return new com.qiniu.storage.Configuration(Region.huanan());
            case "beimei":
                return new com.qiniu.storage.Configuration(Region.beimei());
            default:
                throw new RuntimeException("存储区域配置错误");
        }
    }
}
